package com.momo.mal.dashboard;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.momo.mal.R;
import com.momo.mal.database.MalDatabaseAdapter;
import com.momo.mal.entity.History;
import com.momo.mal.util.DrawableManager;
import com.momo.mal.util.slidingmenu.BaseActivity;

public class Dashboard extends BaseActivity {

	public Dashboard() {
		super(R.string.app_name);
	}
	
	public void update() {
		showAnimeHistory();
		showMangaHistory();
		showStatistic();
	}
		
	private MalDatabaseAdapter dbAdapter;
	private TextView animeWatching, animeCompleted, animeOnHold,
			animeDropped, animePlanToWatch, animeTotal;
	private TextView mangaReading, mangaCompleted, mangaOnHold,
			mangaDropped, mangaPlanToRead, mangaTotal;
	private TextView notifAnime, notifManga;
	private TextView animeTitle1, animeTitle2, animeTitle3, animeDescription1, animeDescription2, animeDescription3, animeUpdate1, animeUpdate2, animeUpdate3;
	private ImageView animeImage1, animeImage2, animeImage3;
	private TextView mangaTitle1, mangaTitle2, mangaTitle3, mangaDescription1, mangaDescription2, mangaDescription3, mangaUpdate1, mangaUpdate2, mangaUpdate3;
	private ImageView mangaImage1, mangaImage2, mangaImage3;
	private DrawableManager drawableManager;
	private SyncReciever reciever;
	private IntentFilter intentFilter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		// setTitle("Momo MAL");

		setContentView(R.layout.main);
		getSupportFragmentManager()
		.beginTransaction()
//		.replace(R.id.content_frame, mContent)
		.commit();

		setSlidingActionBarEnabled(true);

		drawableManager = new DrawableManager(this);
		dbAdapter = new MalDatabaseAdapter(this);

		showAnimeHistory();
		showMangaHistory();
		showStatistic();
		
		reciever = new SyncReciever();
		intentFilter = new IntentFilter("com.momo.mal.action.synchronize");
		
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		registerReceiver(reciever, intentFilter);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(reciever);
	}
	
	public class SyncReciever extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			showAnimeHistory();
			showMangaHistory();
			showStatistic();
		}
		
	}
	
	public void showAnimeHistory() {
		List<History> histories = dbAdapter.getHistory("anime");
		LinearLayout history1 = (LinearLayout) findViewById(R.id.anime_history_1);
		LinearLayout history2 = (LinearLayout) findViewById(R.id.anime_history_2);
		LinearLayout history3 = (LinearLayout) findViewById(R.id.anime_history_3);
		
		notifAnime = (TextView) findViewById(R.id.text_notif_anime);
		animeTitle1 = (TextView) findViewById(R.id.text_title_anime_1);
		animeTitle2 = (TextView) findViewById(R.id.text_title_anime_2);
		animeTitle3 = (TextView) findViewById(R.id.text_title_anime_3);
		animeDescription1 = (TextView) findViewById(R.id.text_description_anime_1);
		animeDescription2 = (TextView) findViewById(R.id.text_description_anime_2);
		animeDescription3 = (TextView) findViewById(R.id.text_description_anime_3);
		animeUpdate1 = (TextView) findViewById(R.id.text_update_date_anime_1);
		animeUpdate2 = (TextView) findViewById(R.id.text_update_date_anime_2);
		animeUpdate3 = (TextView) findViewById(R.id.text_update_date_anime_3);
		animeImage1 = (ImageView) findViewById(R.id.image_anime_1);
		animeImage2 = (ImageView) findViewById(R.id.image_anime_2);
		animeImage3 = (ImageView) findViewById(R.id.image_anime_3);
		
		if(histories.size() == 3) {
			history1.setVisibility(LinearLayout.VISIBLE);
			history2.setVisibility(LinearLayout.VISIBLE);
			history3.setVisibility(LinearLayout.VISIBLE);
			notifAnime.setVisibility(TextView.GONE);
			
			animeTitle1.setText(histories.get(0).getTitle());
			animeTitle2.setText(histories.get(1).getTitle());
			animeTitle3.setText(histories.get(2).getTitle());
			
			animeDescription1.setText(histories.get(0).getDescription());
			animeDescription2.setText(histories.get(1).getDescription());
			animeDescription3.setText(histories.get(2).getDescription());
			
			animeUpdate1.setText(histories.get(0).getUpdateDate());
			animeUpdate2.setText(histories.get(1).getUpdateDate());
			animeUpdate3.setText(histories.get(2).getUpdateDate());
			
			drawableManager.fetchBitmapOnThread(histories.get(0).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(0).getAnimeId() : "manga_" + histories.get(0).getMangaId(), animeImage1);
			drawableManager.fetchBitmapOnThread(histories.get(1).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(1).getAnimeId() : "manga_" + histories.get(1).getMangaId(), animeImage2);
			drawableManager.fetchBitmapOnThread(histories.get(1).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(2).getAnimeId() : "manga_" + histories.get(2).getMangaId(), animeImage3);
		}
		if(histories.size() == 2) {
			history1.setVisibility(LinearLayout.VISIBLE);
			history2.setVisibility(LinearLayout.VISIBLE);
			history3.setVisibility(LinearLayout.GONE);
			notifAnime.setVisibility(TextView.GONE);
			
			animeTitle1.setText(histories.get(0).getTitle());
			animeTitle2.setText(histories.get(1).getTitle());
			
			animeDescription1.setText(histories.get(0).getDescription());
			animeDescription2.setText(histories.get(1).getDescription());
			
			animeUpdate1.setText(histories.get(0).getUpdateDate());
			animeUpdate2.setText(histories.get(1).getUpdateDate());
			
			drawableManager.fetchBitmapOnThread(histories.get(0).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(0).getAnimeId() : "manga_" + histories.get(0).getMangaId(), animeImage1);
			drawableManager.fetchBitmapOnThread(histories.get(1).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(1).getAnimeId() : "manga_" + histories.get(1).getMangaId(), animeImage2);
		}
		if(histories.size() == 1) {
			history1.setVisibility(LinearLayout.VISIBLE);
			history2.setVisibility(LinearLayout.GONE);
			history3.setVisibility(LinearLayout.GONE);
			notifAnime.setVisibility(TextView.GONE);
			
			animeTitle1.setText(histories.get(0).getTitle());
			
			animeDescription1.setText(histories.get(0).getDescription());
			
			animeUpdate1.setText(histories.get(0).getUpdateDate());
			
			drawableManager.fetchBitmapOnThread(histories.get(0).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(0).getAnimeId() : "manga_" + histories.get(0).getMangaId(), animeImage1);
		}
		if(histories.size() == 0) {
			history1.setVisibility(LinearLayout.GONE);
			history2.setVisibility(LinearLayout.GONE);
			history3.setVisibility(LinearLayout.GONE);
			notifAnime.setVisibility(TextView.VISIBLE);
		}
		
	}
	
	public void showMangaHistory() {
		List<History> histories = dbAdapter.getHistory("manga");
		LinearLayout history1 = (LinearLayout) findViewById(R.id.manga_history_1);
		LinearLayout history2 = (LinearLayout) findViewById(R.id.manga_history_2);
		LinearLayout history3 = (LinearLayout) findViewById(R.id.manga_history_3);
		
		notifManga = (TextView) findViewById(R.id.text_notif_manga);
		
		mangaTitle1 = (TextView) findViewById(R.id.text_title_manga_1);
		mangaTitle2 = (TextView) findViewById(R.id.text_title_manga_2);
		mangaTitle3 = (TextView) findViewById(R.id.text_title_manga_3);
		mangaDescription1 = (TextView) findViewById(R.id.text_description_manga_1);
		mangaDescription2 = (TextView) findViewById(R.id.text_description_manga_2);
		mangaDescription3 = (TextView) findViewById(R.id.text_description_manga_3);
		mangaUpdate1 = (TextView) findViewById(R.id.text_update_date_manga_1);
		mangaUpdate2 = (TextView) findViewById(R.id.text_update_date_manga_2);
		mangaUpdate3 = (TextView) findViewById(R.id.text_update_date_manga_3);
		mangaImage1 = (ImageView) findViewById(R.id.image_manga_1);
		mangaImage2 = (ImageView) findViewById(R.id.image_manga_2);
		mangaImage3 = (ImageView) findViewById(R.id.image_manga_3);
		
		if(histories.size() == 3) {
			history1.setVisibility(LinearLayout.VISIBLE);
			history2.setVisibility(LinearLayout.VISIBLE);
			history3.setVisibility(LinearLayout.VISIBLE);
			notifManga.setVisibility(TextView.GONE);
			
			mangaTitle1.setText(histories.get(0).getTitle());
			mangaTitle2.setText(histories.get(1).getTitle());
			mangaTitle3.setText(histories.get(2).getTitle());
			mangaDescription1.setText(histories.get(0).getDescription());
			mangaDescription2.setText(histories.get(1).getDescription());
			mangaDescription3.setText(histories.get(2).getDescription());
			mangaUpdate1.setText(histories.get(0).getUpdateDate());
			mangaUpdate2.setText(histories.get(1).getUpdateDate());
			mangaUpdate3.setText(histories.get(2).getUpdateDate());
			drawableManager.fetchBitmapOnThread(histories.get(0).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(0).getAnimeId() : "manga_" + histories.get(0).getMangaId(), mangaImage1);
			drawableManager.fetchBitmapOnThread(histories.get(1).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(1).getAnimeId() : "manga_" + histories.get(1).getMangaId(), mangaImage2);
			drawableManager.fetchBitmapOnThread(histories.get(1).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(2).getAnimeId() : "manga_" + histories.get(2).getMangaId(), mangaImage3);
		}
		if(histories.size() == 2) {
			history1.setVisibility(LinearLayout.VISIBLE);
			history2.setVisibility(LinearLayout.VISIBLE);
			history3.setVisibility(LinearLayout.GONE);
			notifManga.setVisibility(TextView.GONE);
			
			mangaTitle1.setText(histories.get(0).getTitle());
			mangaTitle2.setText(histories.get(1).getTitle());
			mangaDescription1.setText(histories.get(0).getDescription());
			mangaDescription2.setText(histories.get(1).getDescription());
			mangaUpdate1.setText(histories.get(0).getUpdateDate());
			mangaUpdate2.setText(histories.get(1).getUpdateDate());
			drawableManager.fetchBitmapOnThread(histories.get(0).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(0).getAnimeId() : "manga_" + histories.get(0).getMangaId(), mangaImage1);
			drawableManager.fetchBitmapOnThread(histories.get(1).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(1).getAnimeId() : "manga_" + histories.get(1).getMangaId(), mangaImage2);
		}
		if(histories.size() == 1) {
			history1.setVisibility(LinearLayout.VISIBLE);
			history2.setVisibility(LinearLayout.GONE);
			history3.setVisibility(LinearLayout.GONE);
			notifManga.setVisibility(TextView.GONE);
			
			mangaTitle1.setText(histories.get(0).getTitle());
			mangaDescription1.setText(histories.get(0).getDescription());
			mangaUpdate1.setText(histories.get(0).getUpdateDate());
			drawableManager.fetchBitmapOnThread(histories.get(0).getAnimeId().length() != 0 ? 
					"anime_" + histories.get(0).getAnimeId() : "manga_" + histories.get(0).getMangaId(), mangaImage1);
		}
		if(histories.size() == 0) {
			history1.setVisibility(LinearLayout.GONE);
			history2.setVisibility(LinearLayout.GONE);
			history3.setVisibility(LinearLayout.GONE);
			notifManga.setVisibility(TextView.VISIBLE);
		}
	}
	
	public void showStatistic() {
		animeWatching = (TextView) findViewById(R.id.text_anime_watching);
		animeCompleted = (TextView) findViewById(R.id.text_anime_complete);
		animeOnHold = (TextView) findViewById(R.id.text_anime_on_hold);
		animeDropped = (TextView) findViewById(R.id.text_anime_dropped);
		animePlanToWatch = (TextView) findViewById(R.id.text_anime_plan_to_watch);
		animeTotal = (TextView) findViewById(R.id.text_anime_total);

		mangaReading = (TextView) findViewById(R.id.text_manga_reading);
		mangaCompleted = (TextView) findViewById(R.id.text_manga_complete);
		mangaOnHold = (TextView) findViewById(R.id.text_manga_on_hold);
		mangaDropped = (TextView) findViewById(R.id.text_manga_dropped);
		mangaPlanToRead = (TextView) findViewById(R.id.text_manga_plan_to_read);
		mangaTotal = (TextView) findViewById(R.id.text_manga_total);

		animeWatching.setText("Watching : "
				+ dbAdapter.countAnimeByStatus("watching"));
		animeCompleted.setText("Completed : "
				+ dbAdapter.countAnimeByStatus("completed"));
		animeOnHold.setText("On Hold : "
				+ dbAdapter.countAnimeByStatus("onhold"));
		animeDropped.setText("Dropped : "
				+ dbAdapter.countAnimeByStatus("dropped"));
		animePlanToWatch.setText("Plan to Watch : "
				+ dbAdapter.countAnimeByStatus("plantowatch"));
		animeTotal.setText("Total Entries : " + dbAdapter.countAnime());

		mangaReading.setText("Reading : "
				+ dbAdapter.countMangaByStatus("reading"));
		mangaCompleted.setText("Completed : "
				+ dbAdapter.countMangaByStatus("completed"));
		mangaOnHold.setText("On Hold : "
				+ dbAdapter.countMangaByStatus("onhold"));
		mangaDropped.setText("Dropped : "
				+ dbAdapter.countMangaByStatus("dropped"));
		mangaPlanToRead.setText("Plan to Read : "
				+ dbAdapter.countMangaByStatus("plantoread"));
		mangaTotal.setText("Total Entries : " + dbAdapter.countManga());
	}
}